import lombok.Data;

import java.io.*;
import java.util.Vector;

@Data
public class Recorder {
    public static int getEnNum() {
        return enNum;
    }

    public static int getMyLife() {
        return myLife;
    }

    public static int getAllEnNum() {
        return allEnNum;
    }

    //每关多少敌人
    private static int enNum = 20;
    //生命次数
    private static int myLife = 3;
    //消灭敌人数量
    private static int allEnNum = 0;
    //从文件中回复记录点
    static Vector<Node> nodes = new Vector<>();
    private static FileWriter fileWriter = null;
    private static BufferedWriter bufferedWriter = null;
    private static FileReader fileReader = null;
    private static BufferedReader bufferedReader = null;

    private Vector<EnemyTank> enemyTanks = new Vector<>();

    public Vector<Node> getNodeAndEnNums(String path) {
        try {
            fileReader = new FileReader(path + "/hell_buggy_record.txt");
            fileReader = new FileReader("d:\\myRecording.txt");
            bufferedReader = new BufferedReader(fileReader);
            String n = "";
            //先读一行
            n = bufferedReader.readLine();
            if (n == null || n.equals("")) {
                return nodes;
            }
            allEnNum = Integer.parseInt(n);
            while ((n = bufferedReader.readLine()) != null) {


                String[] xyz = n.split(" ");
                Node node = new Node(Integer.parseInt(xyz[0]), Integer.parseInt(xyz[1]), Integer.parseInt(xyz[2]));
                nodes.add(node);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            try {
                bufferedReader.close();
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return nodes;
    }
    //  保存击毁敌人坦克数量和坦克坐标 方向

    public void keepRecAndEnemyTank(String path) {
        try {
            fileWriter = new FileWriter(path + "/hell_buggy_record.txt");
            fileWriter = new FileWriter("d:\\myRecording.txt");
            bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(allEnNum + "\r\n");
            System.out.println("size=" + enemyTanks.size());
            for (int i = 0; i < enemyTanks.size(); i++) {
                EnemyTank enemyTank = enemyTanks.get(i);
                if (enemyTank.isLive) {
                    //  活得就保存
                    String recode = enemyTank.getX() + " " + enemyTank.getY() + " " + enemyTank.getDirect();

                    bufferedWriter.write(recode + "\r\n");

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedWriter.close();
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }

    public static void getRecording(String path) {
        try {
            fileReader = new FileReader(path + "/hell_buggy_record.txt");
            fileReader = new FileReader("d:\\myRecording.txt");

            bufferedReader = new BufferedReader(fileReader);
            String n = bufferedReader.readLine();
            if (n == null || n.equals("")) {
                return;
            }
            allEnNum = Integer.parseInt(n);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileReader.close();
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    public static void keepRecording(String path) {

        try {
            fileWriter = new FileWriter(path + "/hell_buggy_record.txt");
            fileWriter = new FileWriter("d:\\myRecording.txt");
            bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(allEnNum + "\r\n");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {


            try {
                fileWriter.close();
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void reduceEnNum() {
        enNum--;
    }

    public static void addAllEnNum() {
        allEnNum++;
    }

}
